/**
 * @Title: CarController.java
 * @Package com.qding.park.api.web.service.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:46:52
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.exceptions.system.SystemUnKnown;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.manager.cache.ParkCacheInter;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.facade.core.service.common.CarCommonFacade;
import com.qding.park.manager.rest.conf.SystemConfig;

/**
 * @ClassName: CarController
 * @Description: 车辆 Controller
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:46:52
 *
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/car")
public class CarController extends WebBaseController{

	private static final Log _log = LogFactory.getLog(CarController.class);

	@Autowired
	private CarCommonFacade carCommonFacade;
	
	@Autowired
	private SystemConfig systemConfig;
	
	@Autowired
	private DemoFacade demoFacade;
	
	@Autowired
	private ParkCacheInter parkCacheInter;

	/**
	 * @Description (查询用户名下车辆列表)
	 * @param userId
	 *            北京有户id
	 * @param phone
	 *            手机号
	 * @return
	 */
	@RequestMapping("/myCars")
	public AnswerMsg myCars(String userId, String phone) {
		try {
			/******验证字段********/
			RchlValidateBean vb = new RchlValidateBean();
//			vb.getPhoneValidate().put("phone", phone);
			vb.getRequiredValidate().put("userId", userId);
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult = null;
			if(isDemoRequest(userId)){
				//演示请求
				parkDataResult=demoFacade.queryMyCarsByBjcode(userId);
				
			}else{
				this.clearSessionInfo(userId);
				parkDataResult=carCommonFacade.queryMyCarsByBjcode(userId);
			}
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		}catch(RchlValidatorException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText()+";"+e.getExceptionMessage().getDetailText());
			return answerMsg;
		}
		catch(BaseException e){
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getName()+e.getExceptionMessage().getSimpleText());
			return answerMsg;
		}
		
		catch (Exception e) {
			SystemUnKnown ex=new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
														"未知Exception", "userId="+userId+",phone="+phone));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}

	/**
	 * @Description (查询车辆信息)
	 * @param carNum
	 *            车牌
	 * @return
	 */
	@RequestMapping("/carInfo")
	public AnswerMsg carInfo(String carNum,String userId) {
		try {
			/******验证字段********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("carNum", carNum);
			vb.getRequiredValidate().put("userId", userId);
			
			RchlValidateTools.validate(vb);
			/**************/
			ParkDataResult parkDataResult =null;
			if(isDemoRequest(userId)){
				//演示请求
				parkDataResult=demoFacade.queryCarInfo(userId,carNum);
			}else{
				parkDataResult = carCommonFacade.queryCarInfo(userId,carNum);
			}
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		}catch(RchlValidatorException e){
			
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText()+";"+e.getExceptionMessage().getDetailText());
			return answerMsg;
		} 
		catch (BaseException e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getName());
			return answerMsg;
		}catch (Exception e) {
			SystemUnKnown ex=new SystemUnKnown(e,
					ExceptionMessage.createExceptionMessage(SystemUnKnown.default_Name,
														"CarController.carInfo 未知Exception", "carNum="+carNum));
			exceptionHandle.handle(ex);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR);
			return answerMsg;
		}
	}

}
